import React, { Component } from 'react'

export default class Events extends Component {
  // 这个语法需要一个Webpack插件：@babel/plugin-proposal-class-properties
  state = {
    count: 0
  }

  // constructor() {
  //   super()
  //   this.handleClick = this.handleClick.bind(this)
  // }

  handleClick(num) {
    this.setState(state => ({
      count: state.count + num
    }))
  }

  // 不传参，也定义一个箭头函数
  // handleClick = () => {
  //   this.setState(state => ({
  //     count: state.count + 1
  //   }))
  // }

  // 传参，定义一个函数柯里化，解决传参问题
  handleClick = (num) => {
    return () => {
      this.setState(state => ({
        count: state.count + num
      }))
    }
  }

  render() {
    // console.log(0)
    return (
      <>
        <div>{this.state.count}</div>
        <div><button onClick={this.handleClick}>+</button></div>
      </>
    )
  }
}
